import pickle
import random
class Room:
    def __init__(self,roomid):
        self.roomid = roomid
        self.userdict = {}
        # 记录房间的人员
        self.roomlist = []
    #导入用户信息猜拳游戏.py
    def load_user(self):
        file_name = "userinfo.pickle"
        try:
            with open(file_name,"rb") as fp:
                self.userdict = pickle.load(fp)
        except:
            pass
    #注册用户信息
    def register(self, name, passwd,init_amount):
        if name  not in self.userdict:
            #创建一个新用户
            user = User(name,passwd,init_amount)

            self.userdict[name] = user

            # pickle的方式固化,可以固化对象和自己定义的函数
            file_name = "userinfo.pickle"
            # b是二进制的意思，wb是以二进制打开的意思,而w是以文本的方式打开
            with open(file_name,"wb") as fp:
                pickle.dump(self.userdict,fp)
    # 登录
    def login(self,name,passwd):
        # if name in self.userdict and self.userdict[name].passwd == passwd:
            count = 1
            # 用户可以试错三次
            while count <= 3:
                if name in self.userdict.keys():
                    if self.userdict[name].passwd == passwd:
                        print("登录成功")
                        break
                    else:
                        print("输入密码有误,请重新输入")
                        passwd = input("请重新输入密码")
                        count += 1
                else:
                    print("用户不存在，请注册")
                    break
    # 进房间
    def room_in(self):
        if len(self.roomlist) < 2:
            #
            if  self.userdict[name].user_room == 0 or len(self.userdict[name].user_room) == 0:
                 self.userdict[name].user_room = self.roomid
                 self.roomlist.append(name)
                 print(self.roomlist)
            else:
                print(f"该玩家所在的房间是:{self.userdict[name].user_room}")
                print("房间未满，但是该用户已经加入了其他房间了")
        else:
                print("房间已满，请加入其他的房间")
    #出房间
    def room_out(self):
        if name in self.roomlist:
            self.roomlist.remove(name)
            self.userdict[name].user_room = ""
            print(f"现在该房间人员还剩\t{self.roomlist}")
        else:
            print("玩家不在该房间")
    #换房间
    def swmp_room(self):
        print("当前房间有:")
        for k, v in enumerate(room):
            print(f"{k}.{v.roomid}")
        choice = input("请输入你的选择(按q退出)")
        choice = int(choice)
        current_room = room[choice]
        print(f"现在的游戏房间是\t{room[choice].roomid}")
        return current_room
    # 查看用户信息
    def userinfo(self):
        print(f"当前的用户名是\t{current_room.userdict[name].username}\t".center(20,"*"))
        print(f"当前的余额为\t{current_room.userdict[name].init_amount}\t".center(20, "*"))

class User:
    def __init__(self,username,passwd,init_amount):
        self.username = username
        self.passwd = passwd
        self.init_amount = int(init_amount)
        #这是是否已经进入房间的标志
        self.user_room = ""

    def guess(self,k):
        print(room[k].roomlist)
        if len(room[k].roomlist) ==2:
            name1 = room[k].roomlist[0]
            name2 = room[k].roomlist[1]
            print("游戏即将开始")
            while room[k].userdict[name1].init_amount > 0 and room[k].userdict[name2].init_amount > 0:
                p_choice = input("请输入你的选择.1游戏开始2.退出游戏")
                if p_choice == "1":
                    print("0.石头\t1.布\t2.剪刀".center(20,))
                    #0代表石头，2代表剪刀，1代表布 f代表第一个人，s代表第二个人
                    f= random.randint(0,2)
                    s= random.randint(0,2)
                    print(f"{room[k].roomlist[0]}选择了{f}")
                    print(f"{room[k].roomlist[1]}选择了{s}")
                    if f == s:
                        print("打平")
                    elif f-s == 1 or f-s == -2 :
                        print(f"{room[k].roomlist[0]}赢了")
                        room[k].userdict[name1].init_amount += 100
                        room[k].userdict[name2].init_amount -= 100
                    else:
                        print(f"{room[k].roomlist[1]}赢了")
                        room[k].userdict[name1].init_amount -= 100
                        room[k].userdict[name2].init_amount += 100
                elif p_choice == "2":
                    print(f"{name1}的金豆数是{room[k].userdict[name1].init_amount}、{name2}的金豆数是{room[k].userdict[name2].init_amount}")
                    break
                else:
                    print("输入不合法")
        else:
            print("人数不足,等待其他玩家加入")

room1 = Room("猜拳房间1")
room2 = Room("猜拳房间2")
room3 = Room("猜拳房间3")
room4 = Room("猜拳房间4")

room = [room1,room2,room3,room4]

#主程序
while 1:

    print("当前房间有:")
    for k,v in enumerate(room):
        print(f"{k}.{v.roomid}")
    choice = input("请输入你的选择(按q退出)")
    if choice == "q":
        break
    if choice.isdigit() and int(choice) < len(room):
        choice = int(choice)
        current_room = room[choice]
        current_room.load_user()
        print(f"欢迎进入{current_room.roomid}".center(40,"*"))
        while 1:
            choice1 = input("1.登录\n2.注册\n3.换房间\n请输入你的选择(按q退出)")
            if choice1 == "q":
                break
            if choice1 == "1":
                name,passwd = input("请输入你的名字和、密码（用空白分割）").split()
                current_room.login(name,passwd)
                while 1:
                    choice2 = input("1.加入房间\n2.退出房间\n3.猜拳\n4.查看用户信息\n5.退出\n请输入你的选择".center(20,"*"))
                    if choice2 == "1":
                        current_room.room_in()
                    if choice2 == "2":
                        current_room.room_out()
                    if choice2 == "3":
                        current_room.userdict[name].guess(choice)
                    if choice2 == "4":
                        current_room.userinfo()
                    if choice2 == "5":
                        break
            elif choice1 == "2":
                name,passwd,init_amount = input("请输入你的名字、密码、和初始金额（用空白分割）").split()
                current_room.register(name,passwd,init_amount)
                print(f"当前系统用户有：{current_room.userdict[name]}")
            elif choice1 == "3":
                current_room == current_room.swmp_room()
            else:
                print("输入有误请重新输入")



